package page;

import common.Object;
import common.SearchInput;
import common.User;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.edge.EdgeDriver;

import java.time.Duration;
import java.util.List;

public class FrontPage {
    private EdgeDriver edgeDriver = Object.getEdgeDriver();
    private final String Frontrurl = "http://82.156.186.83:8080/index.html";
    private LoginPage loginPage = new LoginPage();


    public void Search(SearchInput input, User user) throws InterruptedException {
        //这里没有登录就不能使用搜索功能
        loginPage.login(user);
        WebElement searchInput = edgeDriver.findElement(By.cssSelector("#input-search"));
        Thread.sleep(1000);
        searchInput.sendKeys(input.getSearchInput());

        edgeDriver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
        List<WebElement> elements = edgeDriver.findElements(By.cssSelector("#search-down > a"));
        //1
        //> 1

        searchInput.click();
        if (elements.size() > 1) {
            System.out.println("有几个结果");
            System.out.println("查找到了");
        }
        for (WebElement element : elements) {
            System.out.println(element.getText());
        }
    }

    /**
     * 1.点击消息按钮
     * 2.通过获取html class的属性判断点击前后的不同
     * 3.解析字符串，判断是否打开
     * @param user
     * @throws InterruptedException
     */
    public void getMessage(User user) throws InterruptedException {
        loginPage.login(user);
        WebElement getMessageButton = edgeDriver.findElement(By.cssSelector("body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div:nth-child(2) > div > a"));
        Thread.sleep(1000);

        WebElement before = edgeDriver.findElement(By.cssSelector("#index_message_offcanvasEnd"));
        Thread.sleep(1000);
        String bfStr = before.getAttribute("class");
        getMessageButton.click();
        WebElement after = edgeDriver.findElement(By.cssSelector("#index_message_offcanvasEnd"));
        Thread.sleep(1000);
        //多了 show
        String afStr = after.getAttribute("class");
        if (!bfStr.contains("show") && afStr.contains("show")) {
            System.out.println("打开消息页面成功");
        } else {
            System.out.println("打开消息页面失败");
        }
    }

    public void diffBoard(User user) throws InterruptedException {
        loginPage.login(user);

        edgeDriver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
        WebElement boardTitle = edgeDriver.findElement(By.cssSelector("#topBoardList > li:nth-child(3) > a > span"));
        Thread.sleep(2000);
        boardTitle.click();

        WebElement element = edgeDriver.findElement(By.cssSelector("#article_list_count_board"));
        Thread.sleep(2000);
        System.out.println(element.getText());

       boolean frl = element != null && element.getText().length() > 0;
       if (frl){
           System.out.println("内容分类功能正常展示");
       }else{
           System.out.println("内容分类功能不能正常使用");
       }
    }

    public void close() {
        edgeDriver.close();
    }
}
